/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * Copyright (C) 2005-2008 Alfresco Software Limited.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

package node.manager.actions;

import node.manager.alfresco.ServerInfo;
import org.alfresco.webservice.authentication.AuthenticationFault;
import org.alfresco.webservice.repository.RepositoryFault;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLDelete;
import org.alfresco.webservice.types.CMLUpdate;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.Predicate;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;

public class NodeImpl {

    private String node;
    private static NodeImpl instance;
    private String msg = "";
    private String property;
    private String value;

    public static NodeImpl getInstance(ServerInfo serverInfo) throws Exception {
        instance = new NodeImpl(serverInfo);

        return instance;
    }

    private NodeImpl(ServerInfo serverInfo) {
        String url = serverInfo.getHost() + ":" + serverInfo.getPort() + "/alfresco/api";
        WebServiceFactory.setEndpointAddress("http://" + url);

        try {
            AuthenticationUtils.startSession(serverInfo.getUser(), serverInfo.getPass());
        } catch (AuthenticationFault ex) {
            this.msg = ex.getMessage1();
        }
    }

    public void updateNode()
    {
        if (this.getNode().length() > 0 && this.getProperty().length() > 0)
        {
            int pos = node.lastIndexOf("/") + 1;
            String nodeUUID = node.substring(pos, this.node.length());

            try {
                Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
                Reference ref = new Reference(storeRef, nodeUUID, "/app:company_home");

                Predicate predicate = new Predicate(new Reference[]{ref}, null, null);

                pos = this.getProperty().lastIndexOf("}");
                String namespace = this.getProperty().substring(1, pos);
                String name = this.getProperty().substring(pos+1, this.getProperty().length());

                NamedValue properties = Utils.createNamedValue(Constants.createQNameString(namespace, name), this.getValue());
                CMLUpdate update = new CMLUpdate(new NamedValue[]{properties}, predicate, null);

                CML cml = new CML();
                cml.setUpdate(new CMLUpdate[]{update});

                WebServiceFactory.getRepositoryService().update(cml);

                this.msg = "Node updated successfully!";
            } catch (RepositoryFault e) {
                this.msg = "Node delete failed!\n\n" + e.getMessage1();                
            } catch (Exception e) {
                this.msg = "Node delete failed!\n\n" + e.getMessage();
            } finally {
                // End the session
                AuthenticationUtils.endSession();
            }

        }
    }

    public void deleteNode() {

        if (this.node.length() > 0) {
            int pos = node.lastIndexOf("/") + 1;
            String nodeUUID = node.substring(pos, this.node.length());

            try {
                Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
                Reference ref = new Reference(storeRef, nodeUUID, "/app:company_home");

                CMLDelete delete = new CMLDelete(new Predicate(new Reference[]{ref}, null, null));
                CML cml = new CML();
                cml.setDelete(new CMLDelete[]{delete});

                WebServiceFactory.getRepositoryService().update(cml);

                this.msg = "Node deleted successfully!";
            } catch (RepositoryFault e) {
                this.msg = "Node delete failed!\n\n" + e.getMessage1();
            } catch (Throwable e) {
                this.msg = "Node delete failed!\n\n" + e.getMessage();
            } finally {
                // End the session
                AuthenticationUtils.endSession();
            }

        }
    }

    /**
     * @return the node
     */
    public String getNode() {
        return node;
    }

    /**
     * @param node the node to set
     */
    public void setNode(String node) {
        this.node = node;
    }

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * @return the value
     */
    public String getProperty() {
        return property;
    }

    /**
     * @param value the value to set
     */
    public void setProperty(String property) {
        this.property = property;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }
}
